﻿#include "Game_Thread.h"

#include "_Global/Socket/ClientSocket.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Game_Thread::Game_Thread(QObject *parent)
    :GameCore_Thread(parent)
{
}

int Game_Thread::GameInit(int sr)
{
    //m_mutexNet.tryLock();
    //releaseDataList();

    m_Producer.init();

    return GameCore_Thread::GameInit(sr);
}


void Game_Thread::SendDataToServer(QByteArray& data, quint8 outControlbyte)
{
    //m_pGameSocket->send();

    if(m_pGameClientModel->gameStatus() == GameClientModel::Game_Status_Continue
            || m_pGameClientModel->gameStatus() == GameClientModel::Game_Status_GameOver)
        emit s_SendDataToServer(data, outControlbyte);
}

void Game_Thread::sl_qml_DebugButton(int button, QVariant v)
{
    if(button == 1)
    {
    }
    else if(button == 2)
    {
        return;
    }
    else
    {
        qDebug() << "sl_qml_DebugButton:" << button << v;
    }
    return;
}

/*
void Game_Thread::ExampleDeal()
{
    if(m_bMaster)
    {
        if(!GameCore_Thread::Xipai())
            return false;

        if(m_bNetPlay == true)
        {
            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out << static_cast<qint16>(GAME_DATA) << static_cast<qint16>(GAME_DATA_TRANSFER_XIPAI)
                            << static_cast<qint16>(m_nPaiDuiShu)
                            << static_cast<qint16>(sizeof(m_arrayPaiDuiID));
            out.writeRawData(static_cast<const char *>(static_cast<void*>(m_arrayPaiDuiID)), sizeof(m_arrayPaiDuiID));
            SendDataToServer(ba);
            emit s_Debug(_T("洗牌发送数据:"));
        }
    }
    else
    {
        emit s_Debug(_T("等待洗牌:"));

        _GameData *pData = static_cast<_GameData*>(m_Producer.safeOut());
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return false;
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_XIPAI)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("洗牌同步Error:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("洗牌同步Error:nullptr!"));
            setGameStatus(Game_Status_ERROR);
            return false;
        }


        memcpy(m_arrayPaiDuiID, pData->XiPaiData.pData, (size_t)pData->XiPaiData.nDataLength);
        RefreshPaiDui();
        m_nPaiDuiShu = pData->XiPaiData.nData;

        delete pData;

        emit s_Debug(_T("洗牌接受ok"));
    }
    return true;
}*/
